﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AAA.SerialPort.Printer
{
    public abstract class AbstractPrinter : IPrinter
    {
        private ICommand _commandImp;
        private System.IO.Ports.SerialPort _printer;
        private bool _isOpenCashDrawerBeforePrint;
        private bool _isOpenCashDrawerAfterPrint;
            
        public AbstractPrinter()
        {
            _printer = new System.IO.Ports.SerialPort();
            _printer.Encoding = Encoding.Default;
        }
        #region IPrinter 成員

        public ICommand Command
        {
            get
            {
                return _commandImp;
            }
            set
            {
                _commandImp = value;
            }
        }

        public bool OpenCashDrawerBeforePrint
        {
            get
            {
                return _isOpenCashDrawerBeforePrint;
            }
            set
            {
                _isOpenCashDrawerBeforePrint = value;
            }
        }

        public bool OpenCashDrawerAfterPrint
        {
            get
            {
                return _isOpenCashDrawerAfterPrint;
            }
            set
            {
                _isOpenCashDrawerAfterPrint = value;
            }
        }

        public bool ExecuteCommand(string strCommand)
        {
            if (_commandImp != null)
                return _commandImp.SendCommand(_printer, _commandImp.TransferCommand(strCommand));
            return false;
        }

        public bool Init(string strPortName, int iBaudRate, System.IO.Ports.Parity parity, int iDataBits, System.IO.Ports.StopBits stopBits)
        {
            try
            {
                _printer.PortName = strPortName;
                _printer.BaudRate = iBaudRate;
                _printer.Parity = parity;
                _printer.DataBits = iDataBits;
                _printer.StopBits = stopBits;
                _printer.Open();
                return true;
            }
            catch (Exception ex)
            {
            }
            return false;
        }

        public void Write(string strText)
        {
            if (_printer == null)
                return;

            _printer.Write(strText);
        }

        public void Write(byte[] bData, int iOffset, int iLen)
        {
            if (_printer == null)
                return;

            _printer.Write(bData, iOffset, iLen);            
        }

        public void WriteLine(string strText)
        {
            if (_printer == null)
                return;

            _printer.WriteLine(strText);
        }

        protected abstract bool ExecutePrint(object oData);

        public bool Print(object oData)
        {
            
            bool isSuccess = false;

            if (OpenCashDrawerBeforePrint)
            {
                ExecuteCommand(DefaultCommand.OPEN_CASH_DRAWER1);
                ExecuteCommand(DefaultCommand.OPEN_CASH_DRAWER2);
            }

            isSuccess = ExecutePrint(oData);

            if (OpenCashDrawerAfterPrint)
            {
                ExecuteCommand(DefaultCommand.OPEN_CASH_DRAWER1);
                ExecuteCommand(DefaultCommand.OPEN_CASH_DRAWER2);
            }

            return isSuccess;
        }

        #endregion
    }
}
